package com.scalar.forecast.forecastEntry;

import org.apache.struts.util.LabelValueBean;

import java.util.Comparator;
import java.io.Serializable;

import com.scalar.forecast.commons.Constants;

/**
 * User: Sujan
 * Date: Mar 31, 2005
 * Time: 2:30:43 PM
 */
public class BuildNumbersComparator implements Comparator, Serializable
{
	public int compare ( Object buildNumber1, Object buildNumber2 )
	{
		if ( buildNumber1 == null || buildNumber2 == null )
		{
			return 0;
		}
		if ( !( buildNumber1 instanceof LabelValueBean ) || !( buildNumber2 instanceof LabelValueBean ) )
        {
            throw new  ClassCastException ( );
        }
		String buildNo1 = ( ( LabelValueBean )buildNumber1 ).getLabel ();
		String buildNo2 = ( ( LabelValueBean )buildNumber2 ).getLabel ();
		int order1 = 0;
		int order2 = 0;
		try
		{
		 	order1 = Integer.parseInt ( buildNo1.substring ( buildNo1.indexOf ( Constants.FORECAST_BUILD_NO_PREFIX ) + Constants.FORECAST_BUILD_NO_PREFIX.length() ) );
			order2 = Integer.parseInt ( buildNo2.substring ( buildNo2.indexOf ( Constants.FORECAST_BUILD_NO_PREFIX ) + Constants.FORECAST_BUILD_NO_PREFIX.length() ) );
		}
		catch ( NumberFormatException e )
		{
			System.out.println ( "-----Exception in BuildNumbersComparator----" );
			e.printStackTrace();
		}
		catch ( IndexOutOfBoundsException e )
		{
			System.out.println ( "-----Exception in BuildNumbersComparator----" );
			e.printStackTrace();
		}
		int result = 0;
		if( ( order1 == 0 ) && ( order2 == 0 ) )
        {
            result = 0;
        }
        else if( order1 == 0 )
        {
            result = 1;
        }
        else if( order2 == 0 )
        {
            result = -1;
        }
        else
        {
            result = order1 - order2;
        }
        return result;
	}
}
